package com.uwlaser.service.impl;

import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.domain.FactoryModel;
import com.uwlaser.domain.FactoryTreePathInfo;
import com.uwlaser.service.FactoryTreePathInfoService;
import com.uwlaser.service.dto.FactoryTreePathInfoDto;
import com.uwlaser.service.dto.FactoryTreePathInfoQueryParam;
import com.uwlaser.service.mapper.FactoryTreePathInfoMapper;
import com.uwlaser.utils.ConvertUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author Hongchuangbin
* @date 2022-05-20
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = FactoryTreePathInfoService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class FactoryTreePathInfoServiceImpl extends CommonServiceImpl<FactoryTreePathInfoMapper, FactoryTreePathInfo> implements FactoryTreePathInfoService {

    // private final RedisUtils redisUtils;
    private final FactoryTreePathInfoMapper factoryTreePathInfoMapper;

    @Override
    public List<FactoryTreePathInfoDto> queryAll(FactoryTreePathInfoQueryParam query){
        return ConvertUtil.convertList(factoryTreePathInfoMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), FactoryTreePathInfoDto.class);
    }

    @Override
    public List<FactoryModel> queryAllFamily(FactoryTreePathInfo query){
        return factoryTreePathInfoMapper.queryAllFamily(query);
    }

    @Override
    public FactoryTreePathInfo getById(Integer id) {
        return factoryTreePathInfoMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public FactoryTreePathInfoDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), FactoryTreePathInfoDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(FactoryTreePathInfoDto resources) {
        FactoryTreePathInfo entity = ConvertUtil.convert(resources, FactoryTreePathInfo.class);
        return factoryTreePathInfoMapper.insertTreePathInfo(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        return factoryTreePathInfoMapper.delAllChildren(new ArrayList<>(ids));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/
}
